﻿//http://www.jeasyui.com/documentation/treegrid.php
var ModelName = "TOUInfo";
var EmptyRow = [];
var TreeGridTableID = "treeGridTable";
var EditTableID = "editTable";
var TreeIDField = "GID";
var TreePIDField = "PGID";

var IFAddToRoot = false;

$().ready(function () {
    InitEditTable(TreeGridTableID);
});

function InitEditTable() {
    $("#" + TreeGridTableID).find("input").each(function () {
        $(this).blur(function () {
            async = true;
            Validate($(this));
        });
    });
}

function OnRowClick(row, callBack) {
    InitEditTableData(row);
    if (callBack)
        callBack();
}
function Insert(submitingCallBack, submitCallBack, submitedCallBack) {
    async = false;
    isAdd = true;
    if (!OnSubmiting("#" + EditTableID)) {
        return;
    }
    var data = InitNewTreeRow();
    if (submitingCallBack) {
        if (!submitingCallBack(data))
            return;
    }
    AddOrModifyMethod(data[0], true, function (resultData) {
        if (submitCallBack)
            submitCallBack(data, resultData);
        $('#' + TreeGridTableID).treegrid('append', {
            parent: InitParant(),
            data: data
        });
        ClearEditTable(submitedCallBack);
    });
}

function Modify(submitingCallBack, submitCallBack, submitedCallBack) {
    async = false;
    isAdd = false;
    if (!OnSubmiting("#" + EditTableID)) {
        return;
    }
    var node = $('#' + TreeGridTableID).treegrid('getSelected');
    if (submitingCallBack) {
        if (!submitingCallBack(node))
            return;
    }
    if (node && node[TreeIDField]) {
        node = InitRowData(node);
        AddOrModifyMethod(node, false, function (resultData) {
            submitCallBack(node, resultData);
            $('#' + TreeGridTableID).treegrid("refresh", node[TreeIDField]);
            ClearEditTable(submitedCallBack);
        });
    }
}

function InitEditTableData(row, callBack) {
    var tempInput;
    for (var item in row) {
        tempInput = $("#" + ModelName + "_" + item);
        if (CheckNotNull(tempInput) && tempInput.length > 0) {
            tempInput.val(row[item].toString());
        }
    }
    if (CheckNotNull(callBack))
        callBack();
}
function OnSubmiting() {
    var result = true;
    $("#" + EditTableID).find("input").each(function () {
        if (!Validate($(this))) {
            result = false;
            return false;
        }
    });
    return result;
}

function DeletebyID(id) {
    var node = $('#' + TreeGridTableID).treegrid('getSelected');
    if (confirm("系统将删除该节点下全部节点，确定删除吗？")) {
        DeleteMethod(id, true, function (data) {
            $('#' + TreeGridTableID).treegrid('remove', id);
        });
    }
}
function Delete() {
    var node = $('#' + TreeGridTableID).treegrid('getSelected');
    if (node) {
        DeletebyID(node[TreeIDField]);
    }
}

function ClearEditTable(callBack) {
    var inputs = $("#" + EditTableID).find("input");
    inputs.each(function () {
        var input = $(this);
        var type = input.attr("type");
        if (type == "text")
            input.val('');
    });
    if (CheckNotNull(callBack))
        callBack(inputs);
}
function InitNewTreeRow(callBack) {
    var data = InitNewRowData(callBack);
    data[0][TreeIDField] = NewGuid();
    data[0][TreePIDField] = InitPID();
    return data;
}

function InitNewRowData(callBack) {
    var tempInput;
    var data = [];
    var row1 = new Object();
    for (var item in EmptyRow[0]) {
        tempInput = $("#" + ModelName + "_" + item);
        if (CheckNotNull(tempInput) && tempInput.length > 0) {
            row1[item] = tempInput.val();
        }
        else {
            row1[item] = EmptyRow[0][item];
        }
    }
    if (CheckNotNull(callBack))
        callBack();
    data[0] = row1;
    return data;
}

function InitRowData(row) {
    var tempInput;
    for (var item in EmptyRow[0]) {
        tempInput = $("#" + ModelName + "_" + item);
        if (CheckNotNull(tempInput) && tempInput.length > 0) {
            row[item] = tempInput.val();
        }
    }
    return row;
}

function InitParant() {
    if (!IFAddToRoot) {
        var pNode = $('#' + TreeGridTableID).treegrid('getSelected');
        var result = null;
        if (CheckNotNull(pNode)) {
            if (CheckNotNull(pNode[TreeIDField])) {
                result = pNode[TreeIDField];
            }
        }
        return result;
    }
    else
        return "0";
}

function InitPID() {
    if (!IFAddToRoot) {
        var node = $('#' + TreeGridTableID).treegrid('getSelected');
        var pid = "0";
        if (node) {
            pid = node[TreeIDField];
        }
        return pid;
    }
    else
        return "0";
}

function InitPostData(row) {
    var post = EmptyRow[0];
    for (var item in post) {
        post[item] = row[item];
    }
    delete post.state;
    // return post;
    var data = "{";
    for (var item in post) {
        data += item + ":'" + post[item] + "',";
    }
    data = data.slice(0, data.length - 1);
    data = data + "}";
    return data;
}

function AddOrModifyMethod(row, isNew, myCallBack) {
    var postURI = vRoot + "Services/WebService.asmx/Modify" + ModelName;
    if (isNew)
        postURI = vRoot + "Services/WebService.asmx/Add" + ModelName;
    var postData = InitPostData(row);
    $.ajax({
        url: postURI,
        type: "POST",
        data: postData,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function (data) {
            myCallBack(data.d);
        },
        error: function (f) {
        }
    });
}

function DeleteMethod(id, isAll, myCallBack) {
    var postURI = vRoot + "Services/WebService.asmx/Delete" + ModelName;
    var postData = "{" + TreeIDField + ":'" + id + "',isAll:'" + isAll + "'}";
    $.ajax({
        url: postURI,
        type: "POST",
        data: postData,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function (data) {
            myCallBack(data.d);
        },
        error: function (f) {
        }
    });
} 